#include "Stdafx.h"
#include "RFdeviceDefsNet.h"
#include "include\RFDeviceDefs.h"

using namespace RFDeviceNet;

RFdeviceDefsNet::RFdeviceDefsNet()
{
	
}

void RFdeviceDefsNet::NetArrayToNativeArray(array<unsigned char>^ netArray, unsigned char* nativeArray, int size)
{
	for (int i = 0; i < size; i++)
	{		
		nativeArray[i] = netArray[i];
	}
}

void RFdeviceDefsNet::NativeArrayToNetArray(array<unsigned char>^% netArray, unsigned char* nativeArray, int size)
{
	for (int i = 0; i < size; i++)
	{
		netArray[i] = nativeArray[i];
	}
}

//----------------_UDP_DEVINFOBLOCK_PC_Net-------------------------------------

_UDP_DEVINFOBLOCK_PC_Net::_UDP_DEVINFOBLOCK_PC_Net()
{
	IP = gcnew array<unsigned char>(4);
	MAC = gcnew array<unsigned char>(6);
	Info = gcnew array<unsigned char>(256);
}

_UDP_DEVINFOBLOCK_PC_Net::_UDP_DEVINFOBLOCK_PC_Net(RFDevice::_UDP_DEVINFOBLOCK_PC  * pDevBlock)
{
	IP = gcnew array<unsigned char>(4);
	MAC = gcnew array<unsigned char>(6);
	Info = gcnew array<unsigned char>(256);
	if(pDevBlock == NULL)
		return;
	int i = 0;
	for each (unsigned char var in pDevBlock->ucIP)
	{
		IP[i++] = var;
	}
	i = 0;
	for each (unsigned char var in pDevBlock->ucInfo)
	{
		Info[i++] = var;
	}
	i = 0;
	for each (unsigned char var in pDevBlock->ucMAC)
	{
		MAC[i++] = var;
	}
	DeviceType = pDevBlock->usDeviceType;
	pNextDevBlock = pDevBlock->pNext;
	_pDevBlock = pDevBlock;
}

void _UDP_DEVINFOBLOCK_PC_Net::GetDevBlock(RFDevice::_UDP_DEVINFOBLOCK_PC* db)
{
	/*if(_pDevBlock != NULL)
		return _pDevBlock;*/

	//RFDevice::_UDP_DEVINFOBLOCK_PC* db = new RFDevice::_UDP_DEVINFOBLOCK_PC();
	//pin_ptr<RFDevice::_UDP_DEVINFOBLOCK_PC> db = new RFDevice::_UDP_DEVINFOBLOCK_PC();

	if(db == NULL)
		return;
	RFdeviceDefsNet::NetArrayToNativeArray(Info, db->ucInfo, 256);
	RFdeviceDefsNet::NetArrayToNativeArray(IP, db->ucIP, 4);
	RFdeviceDefsNet::NetArrayToNativeArray(MAC, db->ucMAC, 6);
	db->usDeviceType = DeviceType;
	db->pNext = pNextDevBlock;
	_pDevBlock = db;
}

_UDP_DEVINFOBLOCK_PC_Net^ _UDP_DEVINFOBLOCK_PC_Net::GetNext()
{
	_UDP_DEVINFOBLOCK_PC_Net^ next = gcnew _UDP_DEVINFOBLOCK_PC_Net(pNextDevBlock);
	return next;
}

void _UDP_DEVINFOBLOCK_PC_Net::SetNext(_UDP_DEVINFOBLOCK_PC_Net^ devBlock)
{
	delete pNextDevBlock;
	if(devBlock->_pDevBlock != NULL)
	{
		pNextDevBlock = devBlock->_pDevBlock;
		pNextDevBlock->pNext = devBlock->_pDevBlock->pNext;
		return;
	}
	pNextDevBlock = new RFDevice::_UDP_DEVINFOBLOCK_PC();
	RFdeviceDefsNet::NetArrayToNativeArray(devBlock->Info, pNextDevBlock->ucInfo, 256);
	RFdeviceDefsNet::NetArrayToNativeArray(devBlock->IP, pNextDevBlock->ucIP, 4);
	RFdeviceDefsNet::NetArrayToNativeArray(devBlock->MAC, pNextDevBlock->ucMAC, 6);
	pNextDevBlock->usDeviceType = devBlock->DeviceType;
	pNextDevBlock->pNext = devBlock->pNextDevBlock;
}


//-----------------------_RF60x_HELLO_ANSWER_Net-----------------------------------

_RF60x_HELLO_ANSWER_Net::_RF60x_HELLO_ANSWER_Net(_RF60x_HELLO_ANSWER_* origin)
{
	if(origin == NULL)
		return;
	this->DeviceModificaton = origin->bDeviceModificaton;
	this->DeviceType = origin->bDeviceType;	
	this->DeviceMaxDistance = origin->wDeviceMaxDistance;
	this->DeviceRange = origin->wDeviceRange;
	this->DeviceSerial = origin->wDeviceSerial;
}

void _RF60x_HELLO_ANSWER_Net::GetOriginal(_RF60x_HELLO_ANSWER_* retOriginal)
{
	if(retOriginal == NULL)
		retOriginal = new _RF60x_HELLO_ANSWER_();
	retOriginal->bDeviceModificaton = this->DeviceModificaton;
	retOriginal->bDeviceType = this->DeviceType;	
	retOriginal->wDeviceMaxDistance = this->DeviceMaxDistance;
	retOriginal->wDeviceRange = this->DeviceRange;
	retOriginal->wDeviceSerial = this->DeviceSerial;
}


//----------------_RF60x_STREAM_VALUE_Net-------------------------------------

_RF60x_STREAM_VALUE_Net::_RF60x_STREAM_VALUE_Net(_RF60x_STREAM_VALUE_* origin)
{
	if(origin == NULL)
		return;
	this->Measure = origin->wMeasure;
	this->Status = origin->bStatus;
}

 void _RF60x_STREAM_VALUE_Net::GetOriginal(_RF60x_STREAM_VALUE_* retOriginal)
{
	if(retOriginal == NULL)
		retOriginal = new _RF60x_STREAM_VALUE_();
	retOriginal->wMeasure = this->Measure;
	retOriginal->bStatus = this->Status;
}


//----------------_RF60x_UDP_PACKET_Net-------------------------------------

_RF60x_UDP_PACKET_Net::_RF60x_UDP_PACKET_Net()
{
	this->rf60xValArray = gcnew array<_RF60x_STREAM_VALUE_Net^>(168);
}

_RF60x_UDP_PACKET_Net::_RF60x_UDP_PACKET_Net(_RF60x_UDP_PACKET_* origin)
{
	if(origin == NULL)
		return;
	this->rf60xValArray = gcnew array<_RF60x_STREAM_VALUE_Net^>(168);
	this->DeviceSerial = origin->wDeviceSerial;
	this->DeviceBaseDistance = origin->wDeviceBaseDistance;
	this->DeviceMeasureRange = origin->wDeviceMeasureRange;
	this->PackCount = origin->bPackCount;
	this->PacketControlSumm = origin->bPacketControlSumm;
	for(int i = 0; i < 168; i++)
	{
		_RF60x_STREAM_VALUE_Net^ nvalue = gcnew _RF60x_STREAM_VALUE_Net();
		nvalue->Measure = origin->rf60xValArray[i].wMeasure;
		nvalue->Status = origin->rf60xValArray[i].bStatus;
		this->rf60xValArray[i] = nvalue;
	}
}

void _RF60x_UDP_PACKET_Net::GetOriginal(_RF60x_UDP_PACKET_* retOriginal)
{
	if(retOriginal == NULL)
		retOriginal = new _RF60x_UDP_PACKET_();
	retOriginal->wDeviceSerial = this->DeviceSerial;
	retOriginal->wDeviceBaseDistance = this->DeviceBaseDistance;
	retOriginal->wDeviceMeasureRange = this->DeviceMeasureRange;
	retOriginal->bPackCount = this->PackCount;
	retOriginal->bPacketControlSumm = this->PacketControlSumm;
	for(int i = 0; i < 168; i++)
	{
		RF60xUDPVALUE nvalue;
		nvalue.bStatus = this->rf60xValArray[i]->Status;
		nvalue.wMeasure = this->rf60xValArray[i]->Measure;
		retOriginal->rf60xValArray[i] = nvalue;
	}
}